<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 18/12/5
 * Time: 下午2:16
 */

namespace app\index\controller;


use app\index\model\Logs;
use think\Db;
use think\Request;
use think\View;

class Events extends Base
{

    public function add()
    {
        $request = $this->request;
        if ($request->isPost()){
            $data = $request->param();
            $currentUser = session('userLoginInfo');
            $data['uid'] = $currentUser['id'];
            //把数据存入数据库
            if (\app\index\model\Events::create($data)){
                return['code'=>1,'info'=>'添加成功'];
            }else{
                return['code'=>0,'info'=>'添加失败'];
            }
        }
    }

    //删除功能
    public function del()
    {
        $request = $this->request;
        //判断请求类型,并获取请求数据
        if ($request->isPost() && $request->isAjax()) {
            $id = $request->only('id');
            print_r($id);
            $event = \app\index\model\Events::get($id['id']);
            //判断是否是登录用户删除
            if ($event->uid != session('userLoginInfo')['id']) {
                return ['code' => 0, 'info' => '你无权操作'];
            }
            //更改数据库中的数据
            $event->isdelete = 1;
            $event->deletetime = date('Y-m-d H:i:s');

            //储存数据
            if ($event->save()) {
                return ['code' => 1, 'info' => '删除成功'];
            } else {
                return ['code' => 0, 'info' => '删除失败'];
            }
        }
    }

    //查看功能
    public function detal(Request $request)
    {
        if ($request->isGet()){
            $id = $request->only('id');
            $data = \app\index\model\Events::get($id['id']);
//            print_r($data);exit();
            $lie = Db::table('logs')
                    ->where('eid',$id['id'])
                    ->order('id', 'desc')
                    ->select();
            print_r($lie);
//            for($a = 0;$a < $lie.length;$a++){
//                print_r($lie[$a]);
//            }
            $this->assign('lie',$lie);
            $this->assign('data',$data);
            return view();
        }

    }


    //编辑修改功能
    public function modify(Request $request)
    {
        if($request->isPost()){
            //获取post提交的数据
            $data = $request->only('title,content,deadline');
            //获取数据的id
            $id = $request->only('id');
            //获取数据库中的与之相对应的数据
            $ev = \app\index\model\Events::get($id['id']);

            //定义日志修改内容模板
            $logcontent = date('Y-m-d H:i:s');

            //判断是否更改
            if ($ev['title'] != $data['title']){
                $ev->title = $data['title'];
                $logcontent .= '将标题修改为:'.$data['title'];
                print_r($logcontent);
            }
            if($ev['content'] != $data['content']){
               $ev->content = $data['content'];
               $logcontent .= '将内容修改为:'.$data['content'];
            }
            if ($ev['deadline'] != $data['deadline']){
                $ev->deadline = $data['deadline'];
                $logcontent .= '将截止日期更改为'.data['deadline'];
            }
            if ($ev->save()){
                $logData = ['eid'=>$ev->id,'content'=>$logcontent];
                \app\index\model\Logs::create($logData);
                return ['code' => 1 ,'info' => '修改成功'];
            }else{
                return ['code' => 0 , 'info' => '修改失败'];
            }
        }

        if ($request->isGet()){
            $id = $request->only('id');
            $data = \app\index\model\Events::get($id['id']);
            $this->assign('data',$data);
            return view();
        }

    }


    //更改状态功能
    public function changeStatus()
    {
        echo  '更改状态';
    }


}